package components.scenes;

import java.util.List;

import com.uqbar.vainilla.GameScene;

import components.asteroids.Asteroid;
import components.borders.PortBorder;
import components.generator.AsteroidsGenerator;
import components.ship.NormalShip;
import components.ship.Ship;
import components.weapon.Proyectile;

public class MainGameScene extends ScoreableGameScene {

	private PortBorder topBorder;
	
	
	public MainGameScene(int initialScore) {
		super(850, 3, initialScore, 1200, 1200, 20);
		
		this.addBorders();
		this.addShip();
		this.addGenerator();
	}

	private void addGenerator() {
		this.addComponent(new AsteroidsGenerator(this));
		
	}

	private void addShip() {
		Ship s = new NormalShip(300, 300);
		this.addCollisionable(s);
	}

	private void addBorders() {
		PortBorder p1, p2, p3, p4;
		p1 = new PortBorder(PortBorder.SPAWNBOT,800, 30,  0, 0);
		p2 = new PortBorder(PortBorder.SPAWNTOP,800, 30, 0, 970);
		p3 = new PortBorder(PortBorder.SPAWNLEFT, 30, 1000, 800, 0);
		p4 = new PortBorder(PortBorder.SPAWNRIGHT, 30, 1000, 0, 0);
		
		p1.setTargetBorder(p2);
		p2.setTargetBorder(p1);
		p3.setTargetBorder(p4);
		p4.setTargetBorder(p3);
		this.addCollisionable(p1);
		this.addCollisionable(p2);
		this.addCollisionable(p3);
		this.addCollisionable(p4);
		
		this.topBorder = p1;
	}

	public void doShoot(Proyectile proyectile) {
		this.addCollisionable(proyectile);
	}

	public void shipDestroyed(Ship ship) {
		ship.destroyComponent();
		this.loseLife();
		this.addShip();
	}

	public void addAsteroids(List<Asteroid> fragments) {
		for (Asteroid ast: fragments){
			this.addCollisionable(ast);
		}
	}
	public void spawnAsteroids(List<Asteroid> fragments){
		for(Asteroid a : fragments){
			this.addCollisionable(a);
			this.topBorder.spawn(a);
		}
	}
	

	@Override
	public GameScene getGameOverScene(int score) {
		return new EndScene(score);
	}

	public void asteroidDestroyed(Asteroid asteroid) {
		this.addScore(asteroid.getScore());
	}

}
